package com.ciei.dpagm.service.conditionRule.factMode;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ciei.dpagm.common.enums.FactModeEnum;
import com.ciei.dpagm.entity.Parameter;
import com.ciei.dpagm.service.ParameterService;
import com.ciei.dpagm.service.conditionRule.ConditionHandleService;
import com.ciei.dpagm.util.APP;
import com.ciei.dpagm.util.ApplicationContextHolder;
import com.ciei.dpagm.util.DataUtil;
import com.ciei.dpagm.util.JsonUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 所有字段名称
 * @author pp
 * @date 2023/03/14
 */
public class AllKeyNameFactModeService extends AbstractFactModeService {

    private ParameterService parameterService;

    @Override
    public JSONObject analysis(List<String> factModeList, JSONObject originData,FactModeEnum twoFactModeEnum) {
        if(factModeList.size() < 4){
            return ConditionHandleService.ArraySizeNotMatchMessage(FactModeFactoryService.title);
        }
        switch (twoFactModeEnum) {
            case ALL_KEY_NAME_SELECT_PARAMETER:
                return analysisParameterKeySet(factModeList,originData);
            default:
                break;
        }
        return ConditionHandleService.notMatchAnalyzeMessage(FactModeFactoryService.twoFactModeIndex,FactModeFactoryService.title);

    }

    @Override
    protected void conversionOptionList(List<String> factModeList, List<JSONObject> sonFactModeOptionDataList,FactModeEnum twoFactModeEnum) {
        switch (twoFactModeEnum) {
            case ALL_KEY_NAME_SELECT_PARAMETER:
                conversionParameterOptionList(factModeList,sonFactModeOptionDataList,false);
                break;
            default:
                break;
        }
    }

    /**
     * 解析变量字段列表
     * @param factModeList 事实方式列表
     * @param originData 源数据
     * @return
     */
    protected JSONObject analysisParameterKeySet(List<String> factModeList, JSONObject originData){
        parameterService = ApplicationContextHolder.getBean(ParameterService.class);
        List<Parameter> parameterList = parameterService.findByProperty(Parameter::getParameterModuleId, factModeList.get(moduleIdIndex));
        Integer valueIndex = parameterValueIndex;
        JSONObject parameterData = originData;
        while (factModeList.size() > valueIndex){
            Integer finalParameterValue = Integer.valueOf(factModeList.get(valueIndex));
            Parameter nextParameter = parameterList.stream().filter(p -> p.getParameterId().equals(finalParameterValue)).findFirst().orElse(null);
            if(parameterData == null){
                return ConditionHandleService.notFoundParameterDataMessage(FactModeFactoryService.title);
            }
            Object nextParameterValue = parameterData.get(nextParameter.getParameterName());
            if(nextParameterValue == null){
                return ConditionHandleService.originDataNotFoundParameterDataMessage(FactModeFactoryService.title);
            }
            if(nextParameterValue instanceof JSONObject){
                parameterData = parameterData.getJSONObject(nextParameter.getParameterName());
            } else if(nextParameterValue instanceof JSONArray){
                List<Set<String>> resultList = new ArrayList<>();
                JSONArray nextParameterList = parameterData.getJSONArray(nextParameter.getParameterName());
                analyzeList(nextParameterList, resultList, valueIndex, factModeList);
                return JsonUtil.getSuccess("").fluentPut(APP.DATA,resultList);
            }else{
                return ConditionHandleService.notSupportDataTypeMessage();
            }
            valueIndex++;
        }
        return JsonUtil.getSuccess("").fluentPut(APP.DATA,parameterData.keySet());

    }

    /**
     * 解析数组
     * @param parameterList
     * @param resultList
     * @param valueIndex
     * @param factModeList
     */
    public void analyzeList(JSONArray parameterList,List<Set<String>> resultList,Integer valueIndex,List<String> factModeList) {
        Integer nextValueIndex = valueIndex + 2;
        if(factModeList.size() == nextValueIndex) {
            for (int i = 0; i < parameterList.size(); i++) {
                Object value = parameterList.get(i);
                if(value instanceof JSONObject){
                    JSONObject valueData = parameterList.getJSONObject(i);
                    resultList.add(valueData.keySet());
                }
            }
            return;
        }
        for (int i = 0; i < parameterList.size(); i++) {
            Object parameterValue = parameterList.get(i);
            if(parameterValue instanceof JSONObject){
                Parameter nextParameter = parameterService.findById(factModeList.get(nextValueIndex));
                if(nextParameter == null){
                    continue;
                }
                if(nextParameter.getParameterName() == null){
                    continue;
                }
                JSONObject parameterData = parameterList.getJSONObject(i);
                Object nextParameterValue = parameterData.get(nextParameter.getParameterName());
                if (nextParameterValue instanceof JSONObject) {
                    JSONObject nextParameterData = parameterData.getJSONObject(nextParameter.getParameterName());
                    analyzeObject(nextParameterData, resultList, nextValueIndex, factModeList);

                }else if(nextParameterValue instanceof JSONArray){
                    JSONArray sonParameterList = parameterData.getJSONArray(nextParameter.getParameterName());
                    analyzeList(sonParameterList, resultList, nextValueIndex, factModeList);

                }
            }else if(parameterValue instanceof JSONArray){
                for (int j = 0; j < parameterList.size(); j++) {
                    JSONArray sonParameterList = parameterList.getJSONArray(j);
                    analyzeList(sonParameterList, resultList, nextValueIndex, factModeList);
                }
            }
        }
    }
    /**
     * 解析对象
     * @param parameterData
     * @param resultList
     * @param valueIndex
     * @param factModeList
     * @return
     */
    public void analyzeObject(JSONObject parameterData, List<Set<String>> resultList, Integer valueIndex, List<String> factModeList){
        Integer nextValueIndex = valueIndex + 1;
        if(factModeList.size() == nextValueIndex) {
            resultList.add(parameterData.keySet());
        }
        Parameter nextParameter = parameterService.findById(factModeList.get(nextValueIndex));
        Object nextParameterValue = parameterData.get(nextParameter.getParameterName());
        if (nextParameterValue instanceof JSONObject) {
            JSONObject nextParameterData = parameterData.getJSONObject(nextParameter.getParameterName());
            analyzeObject(nextParameterData, resultList, nextValueIndex, factModeList);
        }else if(nextParameterValue instanceof JSONArray){
            JSONArray nextParameterList = parameterData.getJSONArray(nextParameter.getParameterName());
            analyzeList(nextParameterList, resultList, nextValueIndex, factModeList);
        }
    }


}
